home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / MiscKit1.7.1 / MiscKit / Source / MiscKit / MiscUniqueStringArray.m < prev    next >
Encoding:
Text File  |  1994-12-10  |  2.5 KB  |  109 lines

  1. //
  2. //    MiscUniqueStringArray.h -- never stores duplicates of the same string...
  3. //        Written by Bill Bumgarner
  4. //        Copyright (c) 1994 by Bill Bumgarner.
  5. //                Version 1.0.  All rights reserved.
  6. //        This notice may not be removed from this source code.
  7. //
  8. //    This object is included in the MiscKit by permission from the author
  9. //    and its use is governed by the MiscKit license, found in the file
  10. //    "LICENSE.rtf" in the MiscKit distribution.  Please refer to that file
  11. //    for a list of all applicable permissions and restrictions.
  12. //    
  13.  
  14. #import <misckit/misckit.h>
  15.  
  16. @implementation MiscUniqueStringArray:MiscStringArray
  17. - (void) _allocateUniqueStringHashTable
  18. {
  19.     uniqueStringHash = NXCreateHashTableFromZone(NXStrPrototype,
  20.                                                  0,
  21.                                                  NULL,
  22.                                                  [self zone]);
  23. }
  24.  
  25. - init
  26. {
  27.     if (![super init]) return nil;
  28.  
  29.     [self _allocateUniqueStringHashTable];
  30.  
  31.     return self;
  32. }
  33.  
  34. - free
  35. {
  36.     NXFreeHashTable(uniqueStringHash);
  37.     return [super free];
  38. }
  39.  
  40. - addString:(const char *)aString
  41. {
  42.     if(!aString)
  43.         return (nil);
  44.  
  45.     /* NXHashInsert() returns NULL iff. aString is not already in
  46.        the table -- if NXHashInsert() returns NULL, then tell super
  47.        to add the string to the array.  If not, just return self
  48.        [if the caller needs to know whether or not the string was
  49.        already in the table, it is the caller's responsibility to
  50.        check!]
  51.      */
  52.     if( NXHashInsert(uniqueStringHash, aString) == NULL )
  53.         return [super addString:aString];
  54.     else
  55.         return self;
  56. }
  57.  
  58. - removeString:(const char *)aString
  59. {
  60.     if(!aString)
  61.         return (nil);
  62.  
  63.     if( [super removeString:aString] == nil )
  64.         return (nil);
  65.  
  66.     NXHashRemove(uniqueStringHash, aString);
  67.  
  68.     return self;
  69. }
  70.  
  71. - insertString:(const char *)aString at:(unsigned int)index
  72. {
  73.     if(!aString)
  74.         return (nil);
  75.  
  76.     /* same thinking as addString:...
  77.      */
  78.     if( NXHashInsert(uniqueStringHash, aString) == NULL )
  79.         return [super insertString:aString at:index];
  80.     else
  81.         return self;
  82. }
  83.  
  84.  
  85. - (BOOL) isMember:(const char *)aString
  86. {
  87.     return (aString && (NXHashMember(uniqueStringHash, aString) != 0) );
  88. }
  89.  
  90. - awake
  91. {
  92.     id *i, *max;
  93.     
  94.     [self _allocateUniqueStringHashTable];
  95.  
  96.     /* since we KNOW that it was an instance of MiscUniqueStringArray
  97.        that was written, we KNOW that all unarchived strings will
  98.        be unique to this array.  Therefore, rebuild the unique string
  99.        hash be traversing the array and creating hash entries for
  100.        each string.
  101.      */
  102.  
  103.     for (i = NX_ADDRESS(strings), max = i + [strings count]; i<max; i++)
  104.         NXHashInsert(uniqueStringHash, [*i stringValue]);
  105.  
  106.     return self;
  107. }
  108. @end
  109.